<?php

use Email\Email;

/**
 * The Welcome Controller.
 *
 * A basic controller example. Has examples of how to set the
 * response body and status.
 *
 * @package app
 *          @extends Controller
 */
class Controller_Welcome extends Controller
{

    function before ()
    {
        self::set_css('http://linkhay.com/templates/css/common.css');

        self::set_css('main.css');
        self::set_css('jquery.autocomplete.css');
        self::set_css('taotaikhoan.css');
        self::set_css('thongtincanhan.css');
        self::set_css('thongtintaikhoan.css');
    }

    /**
     * The basic welcome message
     *
     * @access public
     * @return Response
     */
    public function action_index ()
    {
        return Response::forge(View::forge('welcome/index.html')->set('css', self::$css, false)
            ->set('js', self::$js, false));
    }

    public function action_smarty ()
    {
        return Response::forge(\View::forge('smarty.html'));
    }

    /**
     * login by vtc account
     */
    public function action_vtc ()
    {
        if (Session::get('user', false)) {
            Response::redirect('/');
        }
        
        Package::load('OAuth');
        
        $provider = new \OAuth\Provider_Vtc();
        
        // neu chua co access token
        
        if (Input::get('code')) {
            // retrieve access token from endpoint
            $provider->getAccessToken();
        } else {
            // redirects to authorize endpoint
            $provider->getCode();
        }
        
        $user = $provider->getUserInfo();
        
        Debug::dump($user);
        die();
    }

    public function action_facebook ()
    {
        Package::load('social');
        
        if (! \Social\Facebook::instance()->check_login()) {
            
            Response::redirect(\Social\Facebook::instance()->login());
        }
        
        Debug::dump(\Social\Facebook::instance()->getUserInfo());
        die();
    }

    public function action_email ()
    {
        Package::load('email');
        
        $mail = Email::forge(array(
                'driver' => 'smtp'
        ));
        
        $mail->set_config('smtp', array(
                
                'host' => 'mail.sgame.vn',
                'port' => 25,
                'timeout' => 5,
                
                'username' => 'no-reply@sgame.vn',
                'password' => 'codaumahoi'
        ));
        
        $mail->from('no-reply@sgame.vn', 'Tien Test');
        
        // Set to
        $mail->to('tien.tran@sgame.vn', 'Phuc Test');
        
        // Work the same for ->cc and ->bcc and ->reply_to
        
        // Set a body message
        
        $email_data['user_name'] = 'test usn';
        $email_data['full_name'] = 'test fn';
        
        $mail->subject('This is my subject');
        // Set a html body message
        $mail->html_body(\View::forge('email/upemail.html', $email_data));
        
        /**
         * By default this will also generate an alt body from the html,
         * and attach any inline files (not paths like http://...)
         */
        
        // Set an alt body
        $mail->alt_body('This is my alt body, for non-html viewers.');
        
        // Set a subject
        $mail->subject('This is the subject');
        
        // Change the priority
        $mail->priority(\Email::P_HIGH);
        
        try {
            
            // And send it
            $result = $mail->send();
        } catch (\EmailSendingFailedException $e) {
            
            // The driver could not send the mail.
            
            Debug::dump($e);
            die();
        } catch (\EmailValidationFailedException $e) {
            
            // One or more email addresses failed validation.
            $these_failed = $mail->get_invalid_addresses();
            
            Debug::dump($e);
            die();
        }
        
        Debug::dump($result);
        die();
    }

    /**
     * A typical "Hello, Bob!" type example.
     * This uses a ViewModel to
     * show how to use them.
     *
     * @access public
     * @return Response
     */
    public function action_hello ()
    {
        return Response::forge(ViewModel::forge('welcome/hello'));
    }

    /**
     * The 404 action for the application.
     *
     * @access public
     * @return Response
     */
    public function action_404 ()
    {
        return Response::forge(ViewModel::forge('welcome/404'), 404);
    }

}
